package com.homejoy.system.service.impl;

import java.util.List;

import com.homejoy.common.utils.DateUtils;
import com.homejoy.common.utils.SecurityUtils;
import com.homejoy.system.domain.HomejoyProperty;
import com.homejoy.system.mapper.HomejoyPropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.homejoy.system.mapper.HomejoyPropertyAuditMapper;
import com.homejoy.system.domain.HomejoyPropertyAudit;
import com.homejoy.system.service.IHomejoyPropertyAuditService;

import javax.annotation.Resource;

/**
 * 房源审核记录Service业务层处理
 *
 * @author homeJoy
 * @date 2025-11-08
 */
@Service
public class HomejoyPropertyAuditServiceImpl implements IHomejoyPropertyAuditService {

    @Resource
    private HomejoyPropertyAuditMapper homejoyPropertyAuditMapper;

    @Resource
    private HomejoyPropertyMapper homejoyPropertyMapper;

    /**
     * 查询房源审核记录
     *
     * @param auditId 房源审核记录主键
     * @return 房源审核记录
     */
    @Override
    public HomejoyPropertyAudit selectHomejoyPropertyAuditByAuditId(Long auditId) {
        return homejoyPropertyAuditMapper.selectHomejoyPropertyAuditByAuditId(auditId);
    }

    /**
     * 查询房源审核记录列表
     *
     * @param homejoyPropertyAudit 房源审核记录
     * @return 房源审核记录
     */
    @Override
    public List<HomejoyPropertyAudit> selectHomejoyPropertyAuditList(HomejoyPropertyAudit homejoyPropertyAudit) {
        List<HomejoyPropertyAudit> homejoyPropertyAudits = homejoyPropertyAuditMapper.selectHomejoyPropertyAuditList(homejoyPropertyAudit);
        homejoyPropertyAudits.forEach(c->{
            HomejoyProperty property = homejoyPropertyMapper.selectHomejoyPropertyByPropertyId(c.getPropertyId());
            c.setPropertyTitle(property.getPropertyTitle());
        });
        return homejoyPropertyAudits;
    }

    /**
     * 新增房源审核记录
     *
     * @param homejoyPropertyAudit 房源审核记录
     * @return 结果
     */
    @Override
    public int insertHomejoyPropertyAudit(HomejoyPropertyAudit homejoyPropertyAudit) {
        return homejoyPropertyAuditMapper.insertHomejoyPropertyAudit(homejoyPropertyAudit);
    }

    /**
     * 修改房源审核记录
     *
     * @param homejoyPropertyAudit 房源审核记录
     * @return 结果
     */
    @Override
    public int updateHomejoyPropertyAudit(HomejoyPropertyAudit homejoyPropertyAudit) {
        return homejoyPropertyAuditMapper.updateHomejoyPropertyAudit(homejoyPropertyAudit);
    }

    @Override
    public int auditHomeJoyProperty(HomejoyPropertyAudit homejoyPropertyAudit) {
        homejoyPropertyAudit.setUpdateBy(SecurityUtils.getUsername());
        homejoyPropertyAudit.setUpdateTime(DateUtils.getNowDate());
        homejoyPropertyAudit.setAuditorName(SecurityUtils.getUsername());
        int i = homejoyPropertyAuditMapper.updateHomejoyPropertyAudit(homejoyPropertyAudit);
        //审核通过后修改房源主表信息的状态
        if(homejoyPropertyAudit.getAuditStatus()==1){
            HomejoyProperty property=new HomejoyProperty();
            property.setPropertyId(homejoyPropertyAudit.getPropertyId());
            property.setPublishStatus(3);
            property.setUpdateBy(SecurityUtils.getUsername());
            property.setUpdateTime(DateUtils.getNowDate());
            //租赁状态
            property.setRentStatus(1);
            HomejoyProperty homejoyProperty = homejoyPropertyMapper.selectHomejoyPropertyByPropertyId(homejoyPropertyAudit.getPropertyId());
            property.setVersion(homejoyProperty.getVersion()+1);
            property.setDisplayStatus(1);
            homejoyPropertyMapper.updateHomejoyProperty(property);
        }
        return i;
    }

    /**
     * 批量删除房源审核记录
     *
     * @param auditIds 需要删除的房源审核记录主键
     * @return 结果
     */
    @Override
    public int deleteHomejoyPropertyAuditByAuditIds(Long[] auditIds) {
        return homejoyPropertyAuditMapper.deleteHomejoyPropertyAuditByAuditIds(auditIds);
    }

    /**
     * 删除房源审核记录信息
     *
     * @param auditId 房源审核记录主键
     * @return 结果
     */
    @Override
    public int deleteHomejoyPropertyAuditByAuditId(Long auditId) {
        return homejoyPropertyAuditMapper.deleteHomejoyPropertyAuditByAuditId(auditId);
    }
}
